/******************************************************************************

	COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2010.

	This file is part of Maxsi Installer.

	Maxsi Installer is free software: you can redistribute it and/or modify it
	under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or (at your
	option) any later version.

	Maxsi Installer is distributed in the hope that it will be useful, but
	WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
	or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
	License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with Maxsi Installer. If not, see <http://www.gnu.org/licenses/>.

	Maxsi Installer
	An installer that downloads and sets up software, data, and games.

	Main.cpp
	Handles the initialization of the UI and main loops.

******************************************************************************/

#include "MaxsiLibrary.h"
#include "UI.h"
#include "PageInitializing.h"
#include "PageInstalling.h"

MESTR*	ApplicationName		=	_MESTR("Maxsi Installer");

MaxsiError OnEvent(MaxsiWindowEvent* Event, BYTE** UserData)
{
	if ( CurrentPage )
	{
		MaxsiError	Result	=	CurrentPage->OnEvent(Event);

		if ( ShouldDestroyPage )
		{
			if (CurrentPage->DeleteControls() != MAXSI_ERROR_SUCCESS )
			{
				WindowSystem->MsgBox(NULL, 0, Translate("MaxsiLibrary/MaxsiWindowCouldNotDeleteControls"), Translate("MaxsiInstaller/MaxsiInstallerWindowTitle"));
			}
			else
			{
				NextPage->SetWindow(CurrentPage->Window);

				CurrentPage		=	NULL;

				if ( NextPage->CreateControls() != MAXSI_ERROR_SUCCESS )
				{
					WindowSystem->MsgBox(NULL, 0, Translate("MaxsiLibrary/MaxsiWindowCouldNotCreateControls"), Translate("MaxsiInstaller/MaxsiInstallerWindowTitle"));
				}
				else
				{
					CurrentPage	=	NextPage;
				}
			}

			return Result;
		}
	}

	return MAXSI_ERROR_NOT_INITIALIZED;
}

int main(int argc, char** argv)
{
	// Initialize the translation system.
	MaxsiError	ConfigurationResult		=	LoadMaxsiComponentConfiguration(_MESTR("MaxsiInstaller"));

	// Create the worker threads.
	if ( CreateWorkerThreadPool() != MAXSI_ERROR_SUCCESS )
	{
		CriticalMsgBox(NULL, 0, _MESTR("MaxsiLibrary/MaxsiWorkerCouldNotInitialize"), ApplicationName);
		return 1;	
	}

	// Get an instance of the local Maxsi Window System used on this platform.
	WindowSystem	=	(MaxsiWindowSystem*)CreateInterface("MaxsiWindowSystem");

	if ( WindowSystem == NULL )
	{
		CriticalMsgBox(NULL, 0, _MESTR("Could not create an instance of the Maxsi Window System."), ApplicationName);
		return 1;	
	}

	// Initialize the Maxsi Window System.
	if ( WindowSystem->Initialize(&argc, &argv) == 0 )
	{
		CriticalMsgBox(NULL, 0, _MESTR("Could not intialize the Maxsi Window System."), ApplicationName);	
		DeleteInterface(WindowSystem);	
		return 1;
	}

	if ( ConfigurationResult != MAXSI_ERROR_SUCCESS )
	{		
		WindowSystem->MsgBox(NULL, 0, Translate("MaxsiLibrary/MaxsiTranslateErrorLoadingConfiguration"), Translate("MaxsiLibrary/MaxsiTranslateErrorHeader"));
	}

	// Create the main Window
	MainWindow	=	WindowSystem->CreateWindow(NULL, _MESTR("Window"), 500, 370, MAXSI_WINDOW_CENTER_X, MAXSI_WINDOW_CENTER_Y, ApplicationName);

	if ( MainWindow == NULL )
	{
		CriticalMsgBox(NULL, 0, _MESTR("Could not create the main window."), ApplicationName);	
		DeleteInterface(WindowSystem);	
		return 1;		
	}

	// Set up the callback function that handles all events on behalf of MainWindow.
	MainWindow->SetEventHandler(OnEvent, NULL);

	CurrentPage		=	new PageInitializing;

	if ( CurrentPage == NULL )
	{
		CriticalMsgBox(NULL, 0, _MESTR("Could not create the main window page."), ApplicationName);	
		WindowSystem->DeleteWindow(MainWindow);
		DeleteInterface(WindowSystem);	
		return 1;		
	}

	CurrentPage->SetWindow(MainWindow);

	// Call the function above that creates all the controls!
	if ( CurrentPage->CreateControls() != MAXSI_ERROR_SUCCESS )
	{
		WindowSystem->MsgBox(NULL, 0, Translate("MaxsiLibrary/MaxsiWindowCouldNotCreateControls"), Translate("MaxsiInstaller/MaxsiInstallerWindowTitle"));	
		WindowSystem->DeleteWindow(MainWindow);
		DeleteInterface(WindowSystem);	
		return 1;	
	}

	// Now that we are done adding controls, it is time to show the window.
	MainWindow->Show();

	// Run the main event loop.
	int	Result = WindowSystem->MainLoop();

	// Clean up!	
	WindowSystem->DeleteWindow(MainWindow);
	DeleteInterface(WindowSystem);

	return Result;
}


